//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Eas.WebCrawler.PlugIns;
using Eas.WebCrawler.Interfaces;
using Eas.WebCrawler.PlugIns.FileNamers;

namespace Eas.WebCrawler.Test.PlugIns {
    /// <summary>
    /// Summary description for FileNamerTest
    /// </summary>
    [TestClass]
    public class FileNamerTest {
        Eas.WebCrawler.PlugIns.FileNamers.FullUrlNamer fullNamer = new Eas.WebCrawler.PlugIns.FileNamers.FullUrlNamer();
        Eas.WebCrawler.PlugIns.FileNamers.SimpleUrlNamer simpleNamer = new Eas.WebCrawler.PlugIns.FileNamers.SimpleUrlNamer();

        public FileNamerTest() {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void TestInitialize() {
            TestDirectoryHelper.Setup();
        }

        //
        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup() {
            TestDirectoryHelper.TearDown();
        }
        //
        #endregion


        [TestMethod]
        /// <summary>Executes the SimpleSingleNameTest test.</summary>
        public void SimpleSingleNameTest() {
            string baseName = "btest.mpg";
            DoSingleTest(simpleNamer, baseName);
        }

        [TestMethod]
        /// <summary>Executes the SimpleMultipleNameTest test.</summary>
        public void SimpleMultipleNameTest() {
            string baseName = "test.mpg";
            DoMultipleFileTest(simpleNamer, baseName);
        }

        [TestMethod]
        /// <summary>Executes the FullSingleNameTest test.</summary>
        public void FullSingleNameTest() {
            string baseName = "www_google_com_b_btest.mpg";
            DoSingleTest(fullNamer, baseName);
        }

        [TestMethod]
        /// <summary>Executes the FullMultipleName test.</summary>
        public void FullMultipleName() {
            string baseName = "www_google_com_a_test.mpg";
            DoMultipleFileTest(fullNamer, baseName);
        }

        [TestMethod]
        /// <summary>Executes the TestNoFileForSimpleNamer test.</summary>
        public void TestNoFileForSimpleNamer() {
            simpleNamer.Initialize(null);
            string name = simpleNamer.GetFileName(new Request("http://www.google.com/"), TestDirectoryHelper.SourceDir);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(name);
            Assert.AreEqual<string>("Default.html", name);
            Assert.AreEqual<string>("Default", fileName);
            name = simpleNamer.GetFileName(new Request("http://www.google.com"), TestDirectoryHelper.SourceDir);
            fileName = System.IO.Path.GetFileNameWithoutExtension(name);
            Assert.AreEqual<string>("Default", fileName);
            name = simpleNamer.GetFileName(new Request("http://www.google.com/default"), TestDirectoryHelper.SourceDir);
            fileName = System.IO.Path.GetFileNameWithoutExtension(name);
            Assert.AreEqual<string>("Default", fileName);
        }


        private void DoSingleTest(FileNamer namer, string baseName) {
            namer.Initialize(null);
            Uri u = new Uri("http://www.google.com/b/btest.mpg");
            Assert.AreEqual<string>(baseName, namer.GetFileName(new Request(u), TestDirectoryHelper.SourceDir));
        }

        private void DoMultipleFileTest(FileNamer namer, string baseName) {
            Uri u = new Uri("http://www.google.com/a/test.mpg");
            Request req = new Request(u);

            string name = namer.GetFileName(req, TestDirectoryHelper.SourceDir);
            Assert.AreEqual<string>(baseName, name);
            TestDirectoryHelper.CreateEmptyFile(TestDirectory.Source, name);

            for (int i = 1; i < 15; ++i) {
                name = namer.GetFileName(req, TestDirectoryHelper.SourceDir);
                Assert.AreEqual<string>(GetName(i, baseName), name);
                TestDirectoryHelper.CreateEmptyFile(TestDirectory.Source, name);
            }
        }

        private static string GetName(int i, string baseName) {
            string ext = System.IO.Path.GetExtension(baseName);
            string file = System.IO.Path.GetFileNameWithoutExtension(baseName);
            return file + "_" + i.ToString() + ext;
        }

    }
}
